<template>
  <div>
    <Nav :title="$route.name"></Nav>
    <div class="container">
      <van-contact-card :type="contactType" @click="onAdd"  :name="currentContact.linkMan"
        :tel="currentContact.mobile"/>
      <van-card
        v-for="item in items" :key="item.key"
        :num="item.number"
        :price="item.price"
        desc=""
        :title="item.name"
        :thumb="item.pic"
      />
    </div>
      <div class="pay_button">
    <p class="pay_num">
      <span class="num">数量
        <b>12</b>
      </span>
      <span class="money">应付金额:
        <b>￥4198</b>
      </span>
    </p>
    <button>支付宝支付</button>
  </div>
  </div>

</template>

<script>
import { fetchAddressList, fechCartStatus } from '_api'
import Nav from '../../components/Nav.vue'
import { getToken } from '_utils/token.js'
export default {
  data () {
    return {
      contactType: 'add', // 联系人状态
      currentContact: {
      },
      items: [],
      price: 0,
      number: 0
    }
  },
  components: { Nav },
  created () {
    this.fetchAddressList()
    // 订单数据
    this.fechCartStatus()
  },

  methods: {
    fetchAddressList () {
      fetchAddressList({
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          this.contactType = 'edit'

          // 有默认 展示默认 没默认展示第一个
          const contacts = res.data.data.result
          const defaultContact = contacts.find(contacts => contacts.isDefault)
          if (defaultContact) {
            this.currentContact = defaultContact
          } else {
            this.currentContact = contacts[0]
          }
        } else {
        }
      })
    },
    onAdd () {
      if (this.contactType === 'add') {
        this.$router.push('/orderAdd')
      } else {
        this.$router.push('/orderAddList')
      }
    },
    // 订单数据
    fechCartStatus () {
      fechCartStatus({
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          // 选中商品
          console.log(res)
          this.price = res.data.price
          this.number = res.data.number
          this.items = res.data.data.items.filter(item => item.selected)
          console.log(this.items)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding-top: 1.2rem;
}
.pay_button{
  background-color: #fff;
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  padding:0 .32rem .32rem .32rem;
  .pay_num{
    font-size: .4rem;
    color: #222;
    display: flex;
    margin:  .266667rem 0;
    justify-content: space-between;
    .num{
      b{
        color: #00aaef;
      }
    }
    .money{
      b{
        color: #f42;
        font-weight: 600;
        font-size: .426667rem;
      }
    }
  }
  button{
    display: block;
    height: 1.2rem;
    width: 100%;
    border:none;
    text-align: center;
    line-height: 1.2rem;
    background-color: #00aaef;
    color: #fff;
    border-radius: .266667rem;
    font-size: .373333rem;
  }
}
.van-card{
  background-color: #fff;
}

</style>
